<template>
  <!--回到顶部-->
  <div ref="backtop"
       :class="[type == 'rectangle'? 'backtop-radius':'backtop-radius-haft','backtop']"
       @click="backTop" >
    <template v-if="type == 'rectangle'">
      <svg-icon icon-class="direction-up"/>
    </template>
    <template v-else>
      <svg-icon icon-class="direction-up"/>
      
      <!-- <div ref="angle" class="angle"></div>
      <div ref="strip" class="strip"></div> -->
    </template>
  </div>
</template>

<script>
    export default {
        props:{
            top:{
                type: String,
                default: () => null
            },
            bottom:{
                type: String,
                default: () => null
            },
            left:{
                type: String,
                default: () => null
            },
            right:{
                type: String,
                default: () => null
            },
            type:{
                type: String,
                default: () => "rectangle" //rectangle、circular
            }
        },
        mounted() {
          this.$refs.backtop.style.display="block"
            this.initPosition();
            addEventListener('scroll', () => {
                let height = parseInt(window.scrollY);
                if (height > 0) {
                    this.$refs.backtop.style.display="block"
                    setTimeout (() => {
                        this.$refs.backtop.style.color="rgba(255, 255, 255, 0.4)"
                        this.$refs.backtop.style.backgroundColor="#1e232e"
                    }, 1)

                }else{
                    this.$refs.backtop.style.color="transparent"
                    this.$refs.backtop.style.backgroundColor="transparent"
                    setTimeout (() => {
                        this.$refs.backtop.style.display="none"
                    }, 300)
                }
            })
        },
        methods: {
            initPosition() {
                if (this.top != null) {
                    this.$refs.backtop.style.top = this.top
                }
                if (this.bottom != null) {
                    this.$refs.backtop.style.bottom = this.bottom
                }
                if (this.left != null) {
                    this.$refs.backtop.style.left = this.left
                }
                if (this.right != null) {
                    this.$refs.backtop.style.right = this.right
                }
            },
            backTop() {
                let timer = setInterval(() => {
                    let height = parseInt(window.scrollY);
                    if (height > 0) {
                        window.scrollTo(0, height-50);
                    }else {
                        clearInterval(timer);
                    }
                }, 1)
            }
        }
    }
</script>

<style scoped>
  .backtop{
    user-select: none;
    cursor: pointer;
    display: none;
    width: 40px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 20px;
    color: transparent;
    border: 1px solid #30363f;
    position: fixed;
    bottom: 10%;
    right: 1%;
    transition: 0.5s;
    z-index: 999;
  }
  .backtop-radius{
    border-radius: 2px;
  }
  .backtop-radius-haft{
    border-radius: 50%;
  }
  .backtop:hover {
    color: rgba(255, 255, 255, 0.6)  !important;
    border: 1px solid #3c434b;
  }


  .angle {
    position: absolute;
    top: -3px;
    left: 10px;
    border-bottom: 10px solid rgba(255, 255, 255, 0.4);
    border-top: 10px solid rgba(255, 255, 255, 0);
    border-right: 10px solid rgba(255, 255, 255, 0);
    border-left: 10px solid rgba(255, 255, 255, 0);
    transition: 0.2s;
  }
  .backtop:hover .angle {
    border-bottom: 10px solid rgba(255, 255, 255, 0.6) !important;
  }
  .strip {
    position: absolute;
    top: 17px;
    left: 17px;
    width: 6px;
    height: 15px;
    background-color: rgba(255, 255, 255, 0.4);
    transition: 0.2s;
  }
  .backtop:hover .strip {
    background-color: rgba(255, 255, 255, 0.6) !important;
  }
</style>
